import 'package:beautiful_demo/common/manager/websocket.dart';
import 'package:beautiful_demo/mvc/user/login/user_bean.dart';
import 'package:drive/drive.dart';

class UserManager {
  static final instance = UserManager();
  static const _privateKey = "chat:common.manager.login_manager";

  UserDataBean _data;

  UserDataBean get data=>_data;

  bool get isLogin =>_data!=null;



  Future init() async {
    final map=await SpUtils.getJson(_privateKey);
    if(map.isNotEmpty){
       _data=UserDataBean(map);
       WebSocketManager.instance.connect();
    }

  }

  void login(UserDataBean data) {
    _data=data;
    WebSocketManager.instance.connect();
    _save();
  }

  void _save() async {
    if(isLogin){
      SpUtils.setJson(_privateKey, _data.toJson());
    }else{
      SpUtils.setJson(_privateKey,{});
    }

  }

  void loginOut() {
    _data=null;
    WebSocketManager.instance.disConnect();
    _save();
  }
}
